//
//  AyBadgeButton.m
//  xWeibo
//
//  Created by alvis mac on 8/1/15.
//  Copyright (c) 2015 alvis. All rights reserved.
//

#import "AyBadgeButton.h"
#import "UIImageExtend.h"

@implementation AyBadgeButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        self.userInteractionEnabled = NO;
        [self setBackgroundImage:[UIImage resizedImageWithName:@"main_badge"] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:11];
    }
    return self;
}


- (void)setBadgeValue:(NSString *)badgeValue{

    //warnnig use copy
    _badgeValue=[badgeValue copy];

    if (badgeValue&&[badgeValue intValue]!=0) {
        self.hidden = NO;
        [self setTitle:badgeValue forState:UIControlStateNormal];
        

        CGRect frame = self.frame;
        CGFloat badgeH = self.currentBackgroundImage.size.height;
        CGFloat badgeW = self.currentBackgroundImage.size.width;
        if (badgeValue.length > 1) {
            CGSize badgeSize = [badgeValue sizeWithFont:self.titleLabel.font];
            badgeW = badgeSize.width + 10;
        }
        frame.size.width = badgeW;
        frame.size.height = badgeH;
        self.frame = frame;
    } else {
        self.hidden = YES;
    }

}

@end
